package com.fyx.controller;


import com.fyx.annotation.Log;
import com.fyx.entity.*;
import com.fyx.service.IGameService;
import com.fyx.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.crypto.Cipher;
import java.io.IOException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Api(tags = "游戏功能相关")
@RestController
@RequestMapping("/api/game")
@RequiredArgsConstructor
public class GameController {
    @Autowired
    private IGameService iGameService;

    @Log("查询玩家投诉/意见")
    @ApiOperation("查询玩家投诉/意见")
    @GetMapping(value="suggest")
    @PreAuthorize("@el.check('suggest:list')")
    public ResponseEntity<Object> suggest(MessageEntity messageEntity, Pageable pageable){
        Map mapResult = iGameService.suggest(messageEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("玩家投诉/意见回复")
    @ApiOperation("玩家投诉/意见回复")
    @PostMapping(value = "sendEmail")
    @PreAuthorize("@el.check('gameUser:sendEmail')")
    public ResponseEntity<Object> sendEmail(@Validated @RequestBody CheckListEntity checkListEntity){
        iGameService.sendEmail(checkListEntity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("查询手机系统统计")
    @ApiOperation("查询手机系统统计")
    @GetMapping(value="phoneInfo")
    @PreAuthorize("@el.check('phoneInfo:list')")
    public ResponseEntity<Object> phoneInfo(PhoneInfo phoneInfo, Pageable pageable){
        Map mapResult = iGameService.phoneInfo(phoneInfo,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("查询cdk兑换码")
    @ApiOperation("查询cdk兑换码")
    @GetMapping(value="cdkCode")
    @PreAuthorize("@el.check('cdkCode:list')")
    public ResponseEntity<Object> cdkCode(CdkDataEntity cdkDataEntity, Pageable pageable){
        Map mapResult = iGameService.cdkCode(cdkDataEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("新增cdk兑换码")
    @ApiOperation("新增cdk兑换码")
    @PostMapping(value = "addCdkCode")
//    @PreAuthorize("@el.check('cdkCode:add')")
    public ResponseEntity<Object> addCdkCode(@Validated @RequestBody CdkDataEntity cdkDataEntity){
        iGameService.addCdkCode(cdkDataEntity);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("广告数据")
    @ApiOperation("广告数据")
    @GetMapping(value="advertising")
    @PreAuthorize("@el.check('advertising:list')")
    public ResponseEntity<Object>  advertising(AdvertisingEntity advertisingEntity, Pageable pageable){
        Map mapResult = iGameService.advertising(advertisingEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("基础数据")
    @ApiOperation("基础数据")
    @GetMapping(value="basisData")
    @PreAuthorize("@el.check('basisData:list')")
    public ResponseEntity<Object> basisData(BasisData basisData, Pageable pageable){
        Map mapResult = iGameService.basisData(basisData,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("玩家注册天数")
    @ApiOperation("玩家注册天数")
    @GetMapping(value="registerDays")
    @PreAuthorize("@el.check('registerDays:list')")
    public ResponseEntity<Object> registerDays(RegisterDays registerDays, Pageable pageable){

        Map mapResult = iGameService.registerDays(registerDays,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("跑马灯列表")
    @ApiOperation("跑马灯列表")
    @GetMapping(value="marquee")
    @PreAuthorize("@el.check('marquee:list')")
    public ResponseEntity<Object> marqueeList(String  msg_title, Pageable pageable){

        Map mapResult = iGameService.marqueeList(msg_title,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }




    @Log("添加跑马灯")
    @ApiOperation("添加跑马灯")
    @PostMapping(value="addMarquee")
    public ResponseEntity<Object> addMarquee(@Validated @RequestBody LampEntity  lampEntity){
        if(iGameService.addMarquee(lampEntity)) {
            return new ResponseEntity<>(HttpStatus.OK);
        }else {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    @DeleteMapping(value = "delMarquee")
    @Log("删除跑马灯")
    @ApiOperation("删除跑马灯")
    public ResponseEntity<Object> delAllInfoLog(@RequestBody Set<String> ids){
        for (String id : ids) {
          iGameService.delMarquee(id);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping(value = "editMarquee")
    @Log("编辑跑马灯")
    @ApiOperation("编辑跑马灯")
    public ResponseEntity<Object> editMarquee(@Validated @RequestBody LampEntity  lampEntity){
        if(iGameService.editMarquee(lampEntity)){
            return new ResponseEntity<>(HttpStatus.OK);
        }
        else{
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    @PostMapping(value = "toMarqueeNotice")
    @Log("通知跑马灯")
    @ApiOperation("通知跑马灯")
    public ResponseEntity<Object> toMarqueeNotice(@RequestBody String id) throws IOException {
        if(iGameService.toMarqueeNotice(id)){
            return new ResponseEntity<>(HttpStatus.OK);
        }
        else{
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    @Log("功能列表")
    @ApiOperation("功能列表")
    @GetMapping(value="ability")
    @PreAuthorize("@el.check('ability:list')")
    public ResponseEntity<Object> ability(String channel,Pageable pageable){
        Map mapResult =  iGameService.abilityList(channel,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("功能列表")
    @ApiOperation("功能列表")
    @GetMapping(value="appAbility")
    @PreAuthorize("@el.check('ability:list')")
    public ResponseEntity<Object> appAbility(Pageable pageable){
        Map mapResult =  iGameService.appAbilityList(pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("修改功能列表")
    @ApiOperation("修改功能列表")
    @PutMapping(value="editAppAbility")
    public ResponseEntity<Object> editAppAbility(@Validated @RequestBody AbilityEntity abilityEntity){
        iGameService.editAppAbility(abilityEntity);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("修改功能列表")
    @ApiOperation("修改功能列表")
    @PutMapping(value="editAbility")
    public ResponseEntity<Object> editAbility(@Validated @RequestBody AbilityEntity abilityEntity){
        iGameService.editAbility(abilityEntity);
        return new ResponseEntity<>(HttpStatus.OK);
    }



    @Log("小游戏列表")
    @ApiOperation("小游戏列表")
    @GetMapping(value="miniGameList")
    @PreAuthorize("@el.check('miniGame:list')")
    public ResponseEntity<Object> miniGameList(MiniGameInfoEntity  miniGameInfoEntity, Pageable pageable){

        Map mapResult = iGameService.miniGameList(miniGameInfoEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }


    @ApiOperation("小游戏列表")
    @GetMapping(value="getMiniGameList")
    public List<MiniGameInfoEntity> getMiniGameList(){
        return   iGameService.getMiniGameList();
    }



    @Log("添加小游戏")
    @ApiOperation("添加小游戏")
    @PostMapping(value="addMiniGame")
    @PreAuthorize("@el.check('miniGame:list')")
    public ResponseEntity<Object> addMiniGame(@Validated @RequestBody MiniGameInfoEntity  miniGameInfoEntity){
        if(iGameService.addMiniGame(miniGameInfoEntity)) {
            return new ResponseEntity<>(HttpStatus.OK);
        }else {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    @DeleteMapping(value = "delMiniGame")
    @Log("删除小游戏")
    @ApiOperation("删除小游戏")
    @PreAuthorize("@el.check('miniGame:list')")
    public ResponseEntity<Object> delMiniGame(@RequestBody Set<String> ids){
        for (String id : ids) {
            iGameService.delMiniGame(id);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping(value = "editMiniGame")
    @Log("编辑小游戏")
    @ApiOperation("编辑小游戏")
    @PreAuthorize("@el.check('miniGame:list')")
    public ResponseEntity<Object> editMiniGame(@Validated @RequestBody MiniGameInfoEntity  miniGameInfoEntity){
        if(iGameService.editMiniGame(miniGameInfoEntity)){
            return new ResponseEntity<>(HttpStatus.OK);
        }
        else{
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }


    @Log("游戏进程")
    @ApiOperation("游戏进程")
    @GetMapping(value="serverConfig")
    @PreAuthorize("@el.check('serverConfig:list')")
    public ResponseEntity<Object> serverConfigList(ServerConfEntity  serverConfEntity, Pageable pageable){
        Map mapResult = iGameService.serverConfigList(serverConfEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

}
